package cn.edu.neosoft.sunbo509.finefood.Fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import java.util.List;

import cn.edu.neosoft.sunbo509.finefood.Adapter.CollectshopAdapter;
import cn.edu.neosoft.sunbo509.finefood.Adapter.MyDividerItemDecoration;
import cn.edu.neosoft.sunbo509.finefood.BaseUrl;
import cn.edu.neosoft.sunbo509.finefood.Bean.GetcollectBean;
import cn.edu.neosoft.sunbo509.finefood.R;
import cn.edu.neosoft.sunbo509.finefood.Service.Service;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * A simple {@link Fragment} subclass.
 */
public class CollectshopFragment extends Fragment {

    private  View view;
    private Retrofit retrofit;
    private String userid;
    private List<GetcollectBean>list;
    private CollectshopAdapter adapter;
    private RecyclerView rv_collectshop;
    private Service service;

    public CollectshopFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view=inflater.inflate(R.layout.fragment_collectshop, container, false);
        Bundle bundle=getArguments();
        userid=bundle.getString("userid");//接受从CollectFragment传递来的userid
        InitRecycleview();
        getDate();
        return view;
    }

    public void InitRecycleview(){
        rv_collectshop=(RecyclerView)view.findViewById(R.id.rv_collectshop);
        GridLayoutManager layoutManager=new GridLayoutManager(getActivity(),1);
        layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                return 1;
            }
        });
        rv_collectshop.setLayoutManager(layoutManager);
        adapter =new CollectshopAdapter(getActivity());
        rv_collectshop.setAdapter(adapter);
        rv_collectshop.addItemDecoration(new MyDividerItemDecoration(getActivity(),GridLayoutManager.VERTICAL));
    }


//获取收藏的店铺信息 flag=0
   public  void getDate(){
       retrofit=new Retrofit.Builder()
               .baseUrl(BaseUrl.BASEURL)
               .addConverterFactory(GsonConverterFactory.create())
               .build();
       service=retrofit.create(Service.class);
       Call<List<GetcollectBean>>call=service.getMessage(Integer.valueOf(userid),0);
       call.enqueue(new Callback<List<GetcollectBean>>() {
           @Override
           public void onResponse(Call<List<GetcollectBean>> call, Response<List<GetcollectBean>> response) {
               list=response.body();
               adapter.setList(list);
               adapter.setUserid(userid);
           }

           @Override
           public void onFailure(Call<List<GetcollectBean>> call, Throwable t) {
               Toast.makeText(getActivity(), "异常", Toast.LENGTH_SHORT).show();
           }
       });
   }


}
